/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package milkyway.hightech.money;

import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * 
 * @author Sanim
 */
public class Money
{
    private Currency currency;
    
    /**
     * The amount of money of this moneyobject
     */
    private BigInteger value = new BigInteger("0");

    /**
     * Constructor
     * @param betrag
     * @param currency 
     */
    public Money(BigInteger betrag, Currency currency)
    {
	this.value = betrag;
	this.currency = currency;
    }

    /**
     *
     * @param money the value to add
     * @return a
     */
    public final Money add(final Money money) {
	return new Money(this.value.add(money.value), currency);
    }

    /**
     * returns a new subtracted money object.
     * @param money the value to subtract
     * @return a subtracted money object
     */
    public final Money subtract(final Money money) {
	return new Money(this.value.subtract(money.value), currency);
    }
    
    public Money convert(Money money)
    {
	money = convertToBaseCurrency(money);
	
	double factor = currency.getFactor();
	BigDecimal d = new BigDecimal(money.getValue());
	d = d.multiply((BigDecimal.valueOf(factor)));
	
	return new Money(d.toBigInteger(), this.currency);
    }
    
    private Money convertToBaseCurrency(Money money)
    {
	double factor = 1 / money.currency.getFactor();
	BigDecimal d = new BigDecimal(money.getValue());
	d = d.multiply(BigDecimal.valueOf(factor));
	
	return new Money(d.toBigInteger(), Currency.EUR);
    }
	
    /**
     * @param money
     * @return boolean
     */
    @Override
    public boolean equals(Object obj)
    {
	if(obj instanceof Money)
	{
	    if(this.value.equals(((Money)obj).value))
		return true;
	}

	return false;
    }
	
    /**
     * 
     * @param m
     * @return boolean
     */
    public boolean greater(Object obj)
    {
	if(obj instanceof Money)
	{
	    Money money =(Money)obj;
	    
	    if(this.value.compareTo(money.value) > 0)
		return true;
	}

	return false;
    }

    /**
     * 
     * @param m
     * @return boolean
     */
    public boolean less(Object obj)
    {
	if(obj instanceof Money)
	{
	    Money money =(Money)obj;
	    
	    if(this.value.compareTo(money.value) < 0)
		return true;
	}

	return false;
    }
    
    public BigInteger getValue()
    {
	return value;
    }
    
    public Currency getCurrency()
    {
	return currency;
    }
}
